<template>
  <div class="app-container">
    <div class="filter-container">
      <excelout />
    </div>
    <div class="filter-container" />
    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">
      <!-- <el-table-column label="ID" prop="id" align="center">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="对账年份" align="center">
        <template slot-scope="{row}">
          <span>{{ row.year }}</span>
        </template>
      </el-table-column>
      <el-table-column label="对账月份" align="center">
        <template slot-scope="{row}">
          <span>{{ row.month }}</span>
        </template>
      </el-table-column>
      <el-table-column label="办单费收入" align="center">
        <template slot-scope="{row}">
          <span>{{ row.service_income }}</span>
        </template>
      </el-table-column>
      <el-table-column label="违约金收入" align="center">
        <template slot-scope="{row}">
          <span>{{ row.chargeback }}</span>
        </template>
      </el-table-column>
      <el-table-column label="买断收入" align="center">
        <template slot-scope="{row}">
          <span>{{ row.buy_income }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租金收益收入" align="center">
        <template slot-scope="{row}">
          <span>{{ row.rate_income }}</span>
        </template>
      </el-table-column>
      <el-table-column label="罚金收入" align="center">
        <template slot-scope="{row}">
          <span>{{ row.fine_income }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总成本" align="center">
        <template slot-scope="{row}">
          <span>{{ row.total_money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="坏账" align="center">
        <template slot-scope="{row}">
          <span>{{ row.baddebt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="本月毛利润" align="center">
        <template slot-scope="{row}">
          <span>{{ row.income }}</span>
        </template>
      </el-table-column>

    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-dialog :visible.sync="dialogFormVisible" :title="textMap[dialogStatus]">

      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 80%; margin-left:70px;">
        <el-form-item label="年份" prop="is_account">
          <el-select v-model="temp.is_account" placeholder="请选择状态">

            <el-option label="对账无误" value="1" />

          </el-select>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          Cancel
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          Confirm
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import excelout from '@/components/excelout/index.vue'
import {
  getProfitBook, profitExcelout
} from '@/api/book'
import {
  formatDate
} from '@/utils/formatDate.js'
import waves from '@/directive/waves' // waves directive
import {
  parseTime
} from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'ProfitTable',
  components: {
    Pagination, excelout
  },
  directives: {
    waves
  },
  filters: {
    formatDate(time) {
      time = time * 1000
      const date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  data() {
    return {
      tableKey: 0,
      tableRecord: 1,
      list: [],
      total: 0,
      listLoading: true,
      dialogStatus: '',
      dialogFormVisible: false,
      listQuery: {
        page: 1,
        limit: 20
      },
      temp: {
        is_account: null
      },
      month_list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
      year_list: [],
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      rules: {
        is_account: [{
          required: true,
          message: '年份必须选择',
          trigger: 'change'
        }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getProfitBook(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 1.0 * 1000)
      })
    },
    excelout() {
      this.listLoading = true
      this.listQuery.limit = this.total
      profitExcelout(this.listQuery).then(response => {
        this.listLoading = false
        const filepath = response.data.path
        window.location.href = filepath
      })
    }
  }
}
</script>

<style>
</style>
